package com.mobzilla.activity;

import java.util.List;

import com.mobzilla.model.Bug;
import com.mobzilla.service.ServiceFactory;
import com.mobzilla.util.UIUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;

public class GridPageActivity extends Activity
{
	int pos;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.gridicons);

		GridView gridview = (GridView) findViewById(R.id.gridview);
		
		if (gridview != null)
		{
			gridview.setAdapter(new ImageAdapter(this, this));			
			gridview.setPadding(40, 100, 40, 0);
		}
	}
	
	public int getPosition()
	{
		return pos;
	}
}

class ImageAdapter extends BaseAdapter
{
	private Context mContext;
	private Activity mActivity;
	private ImageButton[] mButtons;
	private GridButtonListener[] mButtonListeners;
  
	private PopupWindow pw = null;
	private void initiatePopupWindow() {
	    try {
	        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  	        View layout = inflater.inflate(R.layout.popup, (ViewGroup)mActivity.findViewById(R.id.tvEmptyBug));
	        pw = new PopupWindow(layout, 300, 470, true);
	        pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
	        Button okButton = (Button) layout.findViewById(R.id.ok);
	        okButton.setOnClickListener(new OnClickListener() {
	    	    							public void onClick(View v) {
	    	    									pw.dismiss();
	    	    							}});
	 
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}

	public ImageAdapter(Context c, Activity a)
	{
		mContext = c;
		mActivity = a;
		mButtonListeners = new GridButtonListener[]
		{
				new GridButtonListener()
				{
					@Override
					public void onClick(View v) {
						List<Bug> bugList = ServiceFactory.getSearchServiceInstance().fetchAllShowStoppers();
						if (!bugList.isEmpty())
						{
							BugListActivity.setBugList(bugList);
							Intent i = new Intent(mActivity, BugListActivity.class);
							mActivity.startActivity(i);
						}
						else
						{
							initiatePopupWindow();
						}
					}
				},
				new GridButtonListener()
				{
					@Override
					public void onClick(View v) {
						Intent i = new Intent(mActivity, BugListActivity.class);
						mActivity.startActivity(i);									
					}
				},
				new GridButtonListener()
				{
					@Override
					public void onClick(View v) {
						Intent i = new Intent(mActivity, BugListActivity.class);
						mActivity.startActivity(i);									
					}
				},
				new GridButtonListener()
				{
					@Override
					public void onClick(View v) {
						Intent i = new Intent(mActivity, BugListActivity.class);
						mActivity.startActivity(i);								
					}
				},
				new GridButtonListener()
				{
					@Override
					public void onClick(View v) {
						Intent i = new Intent(mActivity, BugProcessor.class);
						mActivity.startActivity(i);	
					}
				},
				new GridButtonListener()
				{
					@Override
					public void onClick(View v) {
						UIUtil.killApp(true);		
					}
				}				
		 };
		
		mButtons = new ImageButton[]
		        {
					new ImageButton(mContext),
					new ImageButton(mContext),
					new ImageButton(mContext),
					new ImageButton(mContext),
					new ImageButton(mContext),
					new ImageButton(mContext)
		        };

		/*mButtons[0].setImageResource(R.drawable.hot);
		mButtons[1].setImageResource(R.drawable.bug);
		mButtons[2].setImageResource(R.drawable.open);
		mButtons[3].setImageResource(R.drawable.resolved);
		mButtons[4].setImageResource(R.drawable.search);
		mButtons[5].setImageResource(R.drawable.close);*/
		
		mButtons[0].setPadding(10, 10, 10, 10);
		mButtons[1].setPadding(10, 10, 10, 10);
		mButtons[2].setPadding(10, 10, 10, 10);
		mButtons[3].setPadding(10, 10, 10, 10);
		mButtons[4].setPadding(10, 10, 10, 10);
		mButtons[5].setPadding(10, 10, 10, 10);
		
		mButtons[0].setOnClickListener(mButtonListeners[0]);
		mButtons[1].setOnClickListener(mButtonListeners[1]);
		mButtons[2].setOnClickListener(mButtonListeners[2]);
		mButtons[3].setOnClickListener(mButtonListeners[3]);
		mButtons[4].setOnClickListener(mButtonListeners[4]);
		mButtons[5].setOnClickListener(mButtonListeners[5]);
		
	}

	public int getCount()
	{
		return mButtons.length;
	}

	public Object getItem(int position)
	{
		return null;
	}

	public long getItemId(int position)
	{
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{	
		return mButtons[position];
	}

}

class GridButtonListener implements OnClickListener
{
	@Override
	public void onClick(View v) {
				
	}
}

